﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Runtime.InteropServices;

public class GameMapFileWindow : EditorWindow
    {

        GameMapData mapData  = new GameMapData();
        public enum MapFileType
        {
            NewMap = 0,
            LoadMap = 1
        }
  
        MapFileType WindowType;
        public static void ShowWindow(string title, MapFileType windowType)
        {
            //创建窗口
            Rect wr = new Rect(300, 200, 560, 320);
            GameMapFileWindow fileWindow = (GameMapFileWindow)EditorWindow.GetWindowWithRect(typeof(GameMapFileWindow), wr, true, title);
            fileWindow.Init(windowType);
            fileWindow.Show();
        }

        public void Init(MapFileType windowType)
        {
            this.WindowType = windowType;
        }

        public void OnGUI()
        {
            switch (WindowType)
            {
                case MapFileType.NewMap:
                    DrawNewMapWindow();
                    break;
                case MapFileType.LoadMap:
                    DrawLoadMapWindow();
                    break;
            }
        }

        bool newMap_IsMapName  = true;
        bool newMap_IsMapPath = true;
        bool newMap_IsMapSize = true;

        void GUI_ColorLabel(string text,Color color) 
        {
            Color oldColor = GUI.backgroundColor;
            GUI.backgroundColor = color;
            GUILayout.Box(text);
            GUI.backgroundColor = oldColor;
        }
        public void DrawNewMapWindow()
        {
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            GUILayout.Label("地图名称", GUILayout.Width(50));
            GUILayout.Space(10);
            mapData.Header.MapName = EditorGUILayout.TextField(mapData.Header.MapName, GUILayout.Width(230));
            GUILayout.Space(5);
            if (!newMap_IsMapName) GUI_ColorLabel("地图名称不可为空",Color.red);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            GUILayout.Label("地图路径", GUILayout.Width(50));
            GUILayout.Space(10);
            mapData.Header.MapPath = EditorGUILayout.TextField(mapData.Header.MapPath, GUILayout.Width(230));
            if (GUILayout.Button("文件路径", GUILayout.Width(80)))
            {
                mapData.Header.MapPath = EditorUtility.SaveFilePanel("保存文件", Application.dataPath + "../Public/", "", "xml");
            }
            GUILayout.Space(5);
            if (!newMap_IsMapPath) GUI_ColorLabel("地图路径不可以为空", Color.red);
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(10);
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            GUILayout.Label("地图宽度", GUILayout.Width(70));

            mapData.Header.MapWidth = EditorGUILayout.IntField(mapData.Header.MapWidth, GUILayout.Width(50));
            GUILayout.Space(30);
            GUILayout.Label("地图高度", GUILayout.Width(70));
            mapData.Header.MapHeight = EditorGUILayout.IntField(mapData.Header.MapHeight, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal(); 

            GUILayout.Space(10);
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            GUILayout.Label("图块宽度", GUILayout.Width(70));
            mapData.Header.TileSizeX = EditorGUILayout.IntField(mapData.Header.TileSizeX, GUILayout.Width(50));
            GUILayout.Space(30);
            GUILayout.Label("图块高度", GUILayout.Width(70));
            mapData.Header.TilesizeY = EditorGUILayout.IntField(mapData.Header.TilesizeY, GUILayout.Width(50));
            GUILayout.Space(5);
            if (!newMap_IsMapSize) GUI_ColorLabel("地图尺寸不可为0",Color.red);
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(10);
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(10);
            GUILayout.Label("描述", GUILayout.Width(70));
            mapData.Header.MapDesc =  EditorGUILayout.TextField(mapData.Header.MapDesc,GUILayout.Width(300),GUILayout.Height(80));
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(30);
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(130);
            Color oldColor = GUI.backgroundColor;
            GUI.backgroundColor = Color.green;
            if (GUILayout.Button("确定", GUILayout.Height(35), GUILayout.Width(200)))
            {
                
                bool IsAllOK =true;
                if (mapData.Header.MapName == null ||mapData.Header.MapName=="") 
                { 
                    newMap_IsMapName = false;
                    IsAllOK = false;
                }

                if (mapData.Header.MapPath==null|| mapData.Header.MapPath == "") 
                {
                    newMap_IsMapPath = false;
                    IsAllOK = false;
                }
                if (mapData.Header.TileSizeX == 0 || mapData.Header.TilesizeY == 0 || mapData.Header.MapWidth == 0 || mapData.Header.MapHeight == 0) 
                {
                    newMap_IsMapSize = false;
                    IsAllOK = false;
                }

                if (IsAllOK) 
                {
                    GameMapFileManager.Inst.SaveGameMap(mapData);
                    this.Close();
                }
            }
            
            GUI.backgroundColor = oldColor;
            EditorGUILayout.EndHorizontal();
        }

        public void DrawLoadMapWindow()
        {
            
        }
    }